package com.ewzj.bigdata.mvp.presenter

import android.support.v4.app.Fragment
import android.support.v7.widget.RecyclerView
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.DecorateContract
import com.ewzj.bigdata.mvp.contract.DecoratePicFragmentContract
import com.ewzj.bigdata.mvp.model.reqbean.Reqpagebean
import com.ewzj.bigdata.mvp.model.rspbean.BaseData
import com.ewzj.bigdata.mvp.model.rspbean.BaseListData
import com.ewzj.bigdata.mvp.model.rspbean.DecPicBean
import com.ewzj.bigdata.mvp.ui.adapter.NewItempicAdapter
import com.jess.arms.mvp.BasePresenter
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */

class DecoratePicPresenter @Inject constructor(val rxErrorHandler: RxErrorHandler,view: DecoratePicFragmentContract.View, model: DecoratePicFragmentContract.Model) :BasePresenter<DecoratePicFragmentContract.Model, DecoratePicFragmentContract.View>(model,view){
    val datas= arrayListOf<DecPicBean>()
    val adapter=NewItempicAdapter(datas)
    var currentpage=1
    var keyword=""
    var context=(mRootView as Fragment).activity
    fun loadFirstPage(){
        currentpage=1
        var req=Reqpagebean()
        if(!keyword.isNullOrEmpty()){
            req.title=keyword
        }
        mModel.decPiclist(req).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<BaseData<BaseListData<DecPicBean>>>(rxErrorHandler){
                    override fun onNext(t: BaseData<BaseListData<DecPicBean>>) {
                        currentpage++
                        datas.clear()
                        datas.addAll(t.data!!.list)
                        if (adapter.emptyView==null){
                            adapter.emptyView= View.inflate(context, R.layout.error_view,null)
                        }
                        adapter.setNewData(datas)
                        if (t.data!!.list.size<AppKey.PAGE_NUM){
                            adapter.disableLoadMoreIfNotFullPage()
                        }

                    }

                })
    }

    fun loadNextdata(){
        var req=Reqpagebean(currentpage)
        if(!keyword.isNullOrEmpty()){
            req.title=keyword
        }
        mModel.decPiclist(req).compose(RxUtils.bindToLifecycle(mRootView))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doAfterTerminate { if (adapter.isLoading){
                    adapter.loadMoreComplete()
                } }
                .subscribe({ t ->run{
                    currentpage++
                    adapter.addData(t.data!!.list)
                    if (t.data!!.list.size<AppKey.PAGE_NUM){
                        adapter.loadMoreEnd()
                    }
                }  }, { adapter.loadMoreFail() })
    }

    fun initAdapter(recyclerView: RecyclerView){
//        adapter.disableLoadMoreIfNotFullPage()
        mRootView.initRecylerView(adapter)
        adapter.setOnLoadMoreListener(object :BaseQuickAdapter.RequestLoadMoreListener{
            override fun onLoadMoreRequested() {
                loadNextdata()
            }

        },recyclerView)

    }
}
